package com.example.qixinshangwutong.view.activity;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.qixinshangwutong.R;

import org.jetbrains.annotations.NotNull;

import cn.leancloud.LCUser;
import cn.leancloud.types.LCNull;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class ModifyActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_modify);

        initView();

    }

    private void initView() {

        EditText modifyUserNameEdit = findViewById(R.id.modify_username_text);

        EditText modifyPhoneCodeEdit = findViewById(R.id.modify_phone_code_text);

        EditText modifyNewPwdEdit = findViewById(R.id.modify_new_pwd_text);

        Button modifyGetPhoneCode = findViewById(R.id.modify_get_phone_code_btn);

        Button modifyBtn = findViewById(R.id.modify_btn);

        modifyGetPhoneCode.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                String phoneNum = modifyUserNameEdit.getText().toString();

                LCUser.requestPasswordResetBySmsCodeInBackground(phoneNum)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCNull>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull LCNull lcNull) {
                                Toast.makeText(ModifyActivity.this,"验证码发送成功",Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {
                                Toast.makeText(ModifyActivity.this,"验证码发送失败",Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onComplete() {

                            }
                        });

            }
        });

        modifyBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {

                String phoneCode = modifyPhoneCodeEdit.getText().toString();

                String newPwd = modifyNewPwdEdit.getText().toString();

                LCUser.resetPasswordBySmsCodeInBackground(phoneCode, newPwd)
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new Observer<LCNull>() {
                            @Override
                            public void onSubscribe(@NotNull Disposable d) {

                            }

                            @Override
                            public void onNext(@NotNull LCNull lcNull) {
                                Toast.makeText(ModifyActivity.this,"密码重置成功",Toast.LENGTH_LONG).show();
                                Intent intent = new Intent(ModifyActivity.this,BottomActivity.class);
                                startActivity(intent);
                            }

                            @Override
                            public void onError(@NotNull Throwable e) {
                                Toast.makeText(ModifyActivity.this,"密码重置失败",Toast.LENGTH_LONG).show();
                            }

                            @Override
                            public void onComplete() {

                            }
                        });
            }
        });

    }

}